<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>健康服务系统</title>
    <meta name="description" content="健康服务系统">
    <meta name="keywords" content="健康服务系统">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <!-- 引入组件库 -->
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index_copy.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <style>
        .datatable {
            position: relative;
            box-sizing: border-box;
            -webkit-box-flex: 1;
            width: 100%;
            max-width: 100%;
            font-size: 14px;
            color: rgb(96, 98, 102);
            overflow: hidden;
            flex: 1 1 0%;
        }

        .datatable td, .datatable th {
            padding: 12px 0;
            min-width: 0;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            text-overflow: ellipsis;
            vertical-align: middle;
            position: relative;
            text-align: left;
        }
    </style>
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>系统设置<small>菜单管理</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统设置</el-breadcrumb-item>
            <el-breadcrumb-item>菜单管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="菜单名称" v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item" @keyup.enter.native="findPage"></el-input>
                <el-button @click="findPage()" class="dalfBut">查询</el-button>
                <!--                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>-->
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="name" label="菜单名称" align="center"></el-table-column>
                <!--<el-table-column label="适用性别" align="center">
                    <template slot-scope="scope">
                        <span>{{ scope.row.sex == '0' ? '不限' : scope.row.sex == '1' ? '男' : '女'}}</span>
                    </template>
                </el-table-column>-->
<!--                <el-table-column prop="linkUrl" label="访问路径" align="center"></el-table-column>-->
                <el-table-column prop="description" label="描述" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 新增标签弹层 -->
            <!--<div class="add-form">
                <el-dialog title="新增菜单" :visible.sync="dialogFormVisible">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form :model="formData" :rules="rules" ref="dataAddForm" label-position="right"
                                         label-width="100px">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="菜单名称" prop="name">
                                                <el-input v-model="formData.name"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="访问路径">
                                                <el-input v-model="formData.linkUrl"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="路径">
                                                <el-input v-model="formData.path"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="优先级" prop="priority">
                                                <el-input v-model="formData.priority"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="图标">
                                                <el-input v-model="formData.icon"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="选择等级" prop="level">
                                                <el-select v-model="formData.level">
                                                    <el-option label="父菜单" value="1"></el-option>
                                                    <el-option label="子菜单" value="2"></el-option>
                                                </el-select>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="选择父菜单" prop="parentMenuId" v-if="formData.level==2">
                                                <el-select v-model="formData.parentMenuId">
                                                    <el-option :label="item.name" v-for="(item,index) in parentMenuList"
                                                               :key="item.id"
                                                               :value="item.id"></el-option>
                                                </el-select>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="描述" prop="description">
                                                <el-input v-model="formData.description" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>
                            <el-tab-pane label="角色信息" name="second">
                                <div class="checkScrol">
                                    <table class="datatable">
                                        <thead>
                                        <tr>
                                            <th>选择</th>
                                            <th>角色名称</th>
                                            <th>角色关键字</th>
                                            <th>角色说明</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <tr v-for="c in tableData">
                                            <td>
                                                <input :id="c.id" v-model="roleIds" type="checkbox" :value="c.id">
                                            </td>
                                            <td><label :for="c.id">{{c.name}}</label></td>
                                            <td><label :for="c.id">{{c.keyword}}</label></td>
                                            <td><label :for="c.id">{{c.description}}</label></td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>-->

            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="编辑菜单" :visible.sync="dialogFormVisibleEdit">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form :model="formData" :rules="rules" ref="dataAddForm" label-position="right"
                                         label-width="100px">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="菜单名称" prop="name">
                                                <el-input v-model="formData.name"/>
                                            </el-form-item>
                                        </el-col>
                                        <!--<el-col :span="12">
                                            <el-form-item label="访问路径">
                                                <el-input v-model="formData.linkUrl"/>
                                            </el-form-item>
                                        </el-col>-->
                                       <!-- <el-col :span="12">
                                            <el-form-item label="路径">
                                                <el-input v-model="formData.path"/>
                                            </el-form-item>
                                        </el-col>-->
                                        <el-col :span="12">
                                            <el-form-item label="优先级" prop="priority">
                                                <el-input v-model="formData.priority"/>
                                            </el-form-item>
                                        </el-col>
                                        <!--<el-col :span="12">
                                            <el-form-item label="图标">
                                                <el-input v-model="formData.icon"/>
                                            </el-form-item>
                                        </el-col>-->
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="选择等级" prop="level">
                                                <el-select v-model="formData.level">
                                                    <el-option label="父菜单" value="1">父菜单</el-option>
                                                    <el-option label="子菜单" value="2">子菜单</el-option>
                                                </el-select>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="选择父菜单" prop="parentMenuId" v-if="formData.level==2">
                                                <el-select v-model="formData.parentMenuId">
                                                    <el-option :label="item.name" v-for="(item,index) in parentMenuList"
                                                               :key="item.id"
                                                               :value="item.id"></el-option>
                                                </el-select>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="描述" prop="description">
                                                <el-input v-model="formData.description" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>
                            <el-tab-pane label="角色信息" name="second">
                                <div class="checkScrol">
                                    <table class="datatable">
                                        <thead>
                                        <tr>
                                            <th>选择</th>
                                            <th>角色名称</th>
                                            <!--                                            <th>角色关键字</th>-->
                                            <th>角色说明</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <tr v-for="c in tableData">
                                            <td>
                                                <input :id="c.id" v-model="roleIds" type="checkbox" :value="c.id">
                                            </td>
                                            <td><label :for="c.id">{{c.name}}</label></td>
                                            <!--                                            <td><label :for="c.id">{{c.keyword}}</label></td>-->
                                            <td><label :for="c.id">{{c.description}}</label></td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisibleEdit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>

<script>


    const vue = new Vue({
        el: '#app',
        data: {
            activeName: 'first',//添加/编辑窗口Tab标签名称
            pagination: {//分页相关属性
                currentPage: 1,
                pageSize: 10,
                total: 100,
                queryString: null,
            },
            dataList: [],//列表数据
            formData: {},//表单数据
            parentMenuList: [],
            tableData: [],//新增和编辑表单中对应的检查项列表数据
            roleIds: [],//新增和编辑表单中角色对应的复选框，基于双向绑定可以进行回显和数据提交
            dialogFormVisible: false,//控制添加窗口显示/隐藏
            dialogFormVisibleEdit: false,//控制编辑窗口显示/隐藏
            rules: {//校验规则
                name: [{required: true, message: '菜单名称为必填项', trigger: 'blur'}],
                priority: [{required: true, message: '优先级为必填项', trigger: 'blur'}],
                level: [{required: true, message: '等级为必选项', trigger: 'change'}],
                parentMenuId: [{required: true, message: '父菜单为必选项', trigger: 'change'}],
                description: [{required: true, message: '描述为必填项', trigger: 'blur'}]
            }
        },
        created() {
            this.findPage();
        },
        mounted() {
            this.findMenuParentMenuId();
        },

        methods: {
            //编辑
            handleEdit() {
                //校验表单输入项是否合法
                this.$refs['dataAddForm'].validate((valid) => {
                    if (!valid) {
                        return false;
                    }
                    if (this.roleIds.length <= 0) {
                        this.$message.warning("请选择角色权限");
                        return;
                    }
                    //表单校验通过，发送请求
                    axios.post("/menu/edit.do?roleIds=" + this.roleIds, this.formData).then((response) => {
                        //隐藏编辑窗口
                        this.dialogFormVisibleEdit = false;
                        if (response.data.flag) {
                            //编辑成功，弹出成功提示信息
                            this.$message({
                                message: response.data.message,
                                type: 'success'
                            });
                        } else {
                            //编辑失败，弹出错误提示信息
                            this.$message.error(response.data.message);
                        }
                    }).catch((error) => {
                        this.showMessage(error.message);
                    }).finally(() => {
                        //重新发送请求查询分页数据
                        this.findPage();
                    });
                })
            },
            async findMenuParentMenuId() {
                let res = await axios.get('/menu/findMenuParentMenuId.do');
                // console.log(res.data.data)
                this.parentMenuList = res.data.data
            },
            //添加
            handleAdd() {
                //校验表单输入项是否合法
                this.$refs['dataAddForm'].validate((valid) => {
                    if (!valid) {
                        return false;
                    }
                    //判断角色信息是否为空
                    if (this.roleIds.length <= 0) {
                        this.$message({
                            message: '角色信息不能为空',
                            type: 'warning'
                        });
                        return;
                    }
                    //表单数据校验通过，发送ajax请求将表单数据提交到后台
                    axios.post("/menu/add.do?roleIds=" + this.roleIds, this.formData).then((response) => {
                        //隐藏新增窗口
                        this.dialogFormVisible = false;
                        //判断后台返回的flag值，true表示添加操作成功，false为添加操作失败
                        if (response.data.flag) {
                            this.$message({
                                message: response.data.message,
                                type: 'success'
                            });
                        } else {
                            this.$message.error(response.data.message);
                        }
                    }).catch((error) => {
                        this.showMessage(error.message);
                    }).finally(() => {
                        //重新查询
                        this.findPage();
                    });
                });


            },
            //分页查询
            async findPage() {
                try {
                    let result = await axios.post("/menu/findPage.do", this.pagination);
                    this.dataList = result.data.rows
                    this.pagination.total = result.data.total
                } catch (error) {
                    this.showMessage(error.message)
                }
            },
            // 重置表单
            resetForm() {
                this.formData = {};
                //重置字段
                if (this.$refs['dataAddForm'] !== undefined) {
                    this.$refs['dataAddForm'].resetFields();
                }
            },
            //权限不足提示
            showMessage(r) {
                if (r == 'Request failed with status code 403') {
                    //权限不足
                    this.$message.error('无使用权限');
                    return false;
                } else {
                    this.$message.error('未知错误');
                    return false;
                }
            },
            // 弹出添加窗口
            handleCreate() {
                //重置表单
                this.resetForm()
                this.dialogFormVisible = true
                //默认切换到第一个标签页（基本信息）
                this.activeName = 'first';
                //查询检查项列表数据
                //重置
                this.checkItemIds = [];
                //发送ajax请求查询所有角色权限信息
                axios.get("/role/findRoleAll.do").then((res) => {
                    if (res.data.flag) {
                        //将检查项列表数据赋值给模型数据用于页面表格展示
                        this.tableData = res.data.data;
                    } else {
                        this.$message.error(res.data.message);
                    }
                });


            },
            // 弹出编辑窗口
            handleUpdate(row) {
                console.log(row)
                this.formData = row

                this.dialogFormVisibleEdit = true
                //默认切换到第一个标签页（基本信息）
                this.activeName = 'first';
                //发送ajax请求查询所有的角色信息，用于检查项表格展示
                axios.get("/role/findRoleAll.do").then((res) => {
                    if (res.data.flag) {
                        //为模型数据赋值，通过VUE数据双向绑定进行信息的回显
                        this.tableData = res.data.data;
                        //查询当前检查组包含的所有检查项id，用于页面回显
                        axios.get("/menu/findRoleIdsByMenuId.do?menuId=" + row.id).then((result) => {
                            // console.log(result.data);
                            //为模型数据赋值，通过VUE数据双向绑定进行信息的回显
                            if (!result.data.flag) {
                                this.$message.error(result.data.message);
                            } else {
                                this.roleIds = result.data.data;
                            }
                        }).catch((e) => {

                        });
                    } else {
                        this.$message.error(res.data.message);
                    }
                });
            },
            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage
                this.findPage()
            },
            // 删除
            handleDelete(row) {
                this.$confirm('确认删除吗？', '提示', {type: 'warning'}).then(async () => {
                    let result = await axios.get("/menu/delete.do?id=" + row.id);
                    if (!result.data.flag) {
                        //删除失败
                        this.$message.error(result.data.message)
                    } else {
                        //删除成功
                        this.$message({
                            message: result.data.message,
                            type: 'success'
                        });
                        await this.findPage();

                    }
                }).catch((error) => {
                    this.showMessage(error.message);
                })
            }
        }
    })
</script>
</html>
